package com.qdu.spring.blog.controller.test1;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qdu.spring.blog.bean.test1.User1;
import com.qdu.spring.blog.bean.test1.User1Example;
import com.qdu.spring.blog.service.test1.User1Service;
import com.qdu.spring.blog.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/test1")
public class User1Controller {
    @Autowired
    private User1Service user1Service;
//    插入一名user
    @PostMapping("/insert")
    public AjaxResult insert(User1 user1){
        int i = 1/user1.getAge();//测试全局事务
        if (user1Service.insertSelective(user1)==1) {
            return AjaxResult.successInstance("插入成功");
        }
        else
            return AjaxResult.errorInstance("插入失败");
    }

    //根据主键id选择一条user记录
    @RequestMapping("/select")
    public User1 select(Integer id){
        return user1Service.selectByPrimaryKey(id);
    }

    //查询所有用户
    @RequestMapping("/selectAll")
    public List<User1> selectAll(){
        return user1Service.selectAll();
    }

    //根据年龄查出符合该条件的所有用户信息
    @RequestMapping("/selectAllByAge")
    public List selectAllByAge(Integer age){
        User1Example user1Example = new User1Example();
        user1Example.createCriteria().andAgeEqualTo(age);
        return user1Service.selectByExample(user1Example);
    }

    /**
     * page:当前页数
     * pageSize:当前展示多少个
     */
    //分页查询所有User
    @RequestMapping("selectUserByPage")
    public PageInfo<User1> selectUser(int page,int pageSize){
        PageHelper.startPage(page,pageSize);
        List<User1> user1s = user1Service.selectAll();
        PageInfo<User1> user1PageInfo = new PageInfo<>(user1s);
        return user1PageInfo;
    }
    //通过Service层分页查询所有User
//    @RequestMapping("selectUserByPage")
//    public PageInfo<User1> selectUser(int page,int pageSize){
//        return user1Service.selectUser(page,pageSize);
//    }


}

